﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.IO;

namespace CoffeeScriptHandler
{
    public class CoffeeScriptCompiler
    {
        private readonly ScriptReader _reader;
        private readonly TempScriptWriter _writer;

        public CoffeeScriptCompiler(ScriptReader reader, TempScriptWriter writer)
        {
            _reader = reader;
            _writer = writer;
        }

        public string EvaluateScript(string fileName)
        {
            var tempScriptFileName = _writer.Write(_reader.ReadCoffeeScript(fileName));
            ProcessStartInfo info = new ProcessStartInfo("Cscript", tempScriptFileName + " //Nologo");
            info.UseShellExecute = false;
            info.RedirectStandardOutput = true;

            Process process = Process.Start(info);
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
            return result;
        }


    }
}
